package com.ruoyi.web.controller.system;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.shanxi.JsonRootBean;
import com.ruoyi.system.domain.shanxi.StationPowerList;
import com.ruoyi.system.service.*;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电站
 *
 * @author
 */
@RestController
@RequestMapping("/shanxi")
public class ShanxiController extends BaseController {
    @Resource
    IStationPowerService stationPowerService;
    @Resource
    IStationRealKpiService stationRealKpiService;
    @Resource
    IStationHourKpiService stationHourKpiService;
    @Resource
    IStationDayKpiService stationDayKpiService;
    @Resource
    IStationMonthKpiService stationMonthKpiService;
    @Resource
    IStationYearKpiService stationYearKpiService;
    @Resource
    IDevicesService devicesService;
    @Resource
    IDeviceInfo1Service deviceInfo1Service;
    @Resource
    IDeviceInfo10Service deviceInfo10Service;
    @Resource
    IDeviceInfo17Service deviceInfo17Service;
    @Resource
    IDeviceInfo38Service deviceInfo38Service;
    @Resource
    IDeviceInfo39Service deviceInfo39Service;
    @Resource
    IDeviceInfo41Service deviceInfo41Service;
    @Resource
    IDeviceInfo47Service deviceInfo47Service;
    @Resource
    IDeviceAlarmsService deviceAlarmsService;
    @Resource
    IDeviceDayInfo1Service deviceDayInfo1Service;
    @Resource
    IDeviceDayInfo39Service deviceDayInfo39Service;


    private String domain = "https://cn.fusionsolar.huawei.com/";
    private String tokenName = "XSRF-TOKEN";
    private String xsrfToken = "x-fu9jhfddljhc6nryurtgdhtcinapph6k1f84vufwk606k4ljvuqkrto87u2lao85mlpf06kaep5gupg684erarjsk88bilpjtg04nug9pgvumplf86te9f6mmqlig4ju";

    //登录
    @GetMapping("/login")
    public AjaxResult login() {
        //XSRF-TOKEN过期时间为30分钟
        String url = domain + "thirdData/login";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("userName", "长高智产光伏电站");
        datamap.put("systemCode", "cgdl123456");
        HttpResponse res = HttpRequest.post(url)
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute();
        String token = res.header(tokenName);
        xsrfToken = token;
        String result = res.body();
        return success(token);
    }

    //注销
    @GetMapping("/logout")
    public AjaxResult logout() {
        String url = domain + "thirdData/logout";
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .timeout(20000)//超时，毫秒
                .execute().body();
        return success(result);
    }

    //电站列表
    @GetMapping("/list")
    public AjaxResult list(@ApiParam(name = "pageNo", value = "页码", required = false) @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                           @ApiParam(name = "gridConnectedStartTime", value = "并网开始时间", required = false) @RequestParam(value = "gridConnectedStartTime", defaultValue = "0") Integer gridConnectedStartTime,
                           @ApiParam(name = "gridConnectedEndTime", value = "并网结束时间", required = false) @RequestParam(value = "gridConnectedEndTime", defaultValue = "0") Integer gridConnectedEndTime) {
        String url = domain + "thirdData/stations";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("pageNo", pageNo);
        if (gridConnectedStartTime > 0) {
            datamap.put("gridConnectedStartTime", "onClick$Logout");
        }
        if (gridConnectedEndTime > 0) {
            datamap.put("gridConnectedEndTime", "onClick$Logout");
        }
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        TypeReference<JsonRootBean<StationPowerList>> typeRef = new TypeReference<JsonRootBean<StationPowerList>>() {
        };
        JsonRootBean<StationPowerList> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            for (StationPower item : jsonRootBean.getData().getList()) {
                stationPowerService.insertStationPower(item);
            }
        }
        return success(result);
    }

    //电站实时数据接口
    @GetMapping("/getStationRealKpi")
    public AjaxResult getStationRealKpi(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes) {
        //每5分钟限流次数
        //= 向上取整（电站个数/100）。
        //支持最大并发数1分钟1次。
        //在使用过程中如果超过访问频度的限制，接口会返回错误码407。

        String url = domain + "thirdData/getStationRealKpi";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        result=replaceJson(result);
        TypeReference<JsonRootBean<List<StationRealKpiRes<StationRealKpi>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<StationRealKpi>>>>() {
        };

        JsonRootBean<List<StationRealKpiRes<StationRealKpi>>> jsonRootBean = JSON.parseObject(result, typeRef);
        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<StationRealKpi>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<StationRealKpi> stationRealKpis = item.getDataItemMap();
                for (StationRealKpi stationRealKpi : stationRealKpis) {
                    stationRealKpi.setStationCode(item.getStationCode());
                    stationRealKpiService.insertStationRealKpi(stationRealKpi);
                }
            }
        }
        return success(result);
    }

    protected String replaceJson(String result){
        String[] arr = result.split("_");
        StringBuilder sb = new StringBuilder();
        sb.append(arr[0]);
        for (int i = 1; i < arr.length; i++) {
            String first = arr[i].substring(0, 1).toUpperCase();
            String rest = arr[i].substring(1);
            sb.append(first);
            sb.append(rest);
        }
        return sb.toString();
    }

    //电站小时数据接口
    @GetMapping("/getKpiStationHour")
    public AjaxResult getKpiStationHour(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes,
                                        @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getKpiStationHour";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        datamap.put("collectTime", collectTime);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        result=replaceJson(result);
        TypeReference<JsonRootBean<List<StationRealKpiRes<StationHourKpi>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<StationHourKpi>>>>() {
        };

        JsonRootBean<List<StationRealKpiRes<StationHourKpi>>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<StationHourKpi>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<StationHourKpi> stationHourKpis = item.getDataItemMap();
                for (StationHourKpi stationHourKpi : stationHourKpis) {
                    stationHourKpi.setStationCode(item.getStationCode());
                    Date resCollectTime = DateUtil.date(item.getCollectTime());
                    stationHourKpi.setCollectTime(resCollectTime);
                    stationHourKpiService.insertStationHourKpi(stationHourKpi);
                }
            }
        }
        return success(result);
    }

    //电站日数据接口
    @GetMapping("/getKpiStationDay")
    public AjaxResult getKpiStationDay(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes,
                                       @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getKpiStationDay";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        datamap.put("collectTime", collectTime);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        result=replaceJson(result);
        TypeReference<JsonRootBean<List<StationRealKpiRes<StationDayKpi>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<StationDayKpi>>>>() {
        };

        JsonRootBean<List<StationRealKpiRes<StationDayKpi>>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<StationDayKpi>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<StationDayKpi> stationDayKpis = item.getDataItemMap();
                for (StationDayKpi stationDayKpi : stationDayKpis) {
                    stationDayKpi.setStationCode(item.getStationCode());
                    Date resCollectTime = DateUtil.date(item.getCollectTime());
                    stationDayKpi.setCollectTime(resCollectTime);
                    stationDayKpiService.insertStationDayKpi(stationDayKpi);
                }
            }
        }
        return success(result);
    }

    //电站月数据接口
    @GetMapping("/getKpiStationMonth")
    public AjaxResult getKpiStationMonth(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes,
                                         @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getKpiStationMonth";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        datamap.put("collectTime", collectTime);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        result=replaceJson(result);
        TypeReference<JsonRootBean<List<StationRealKpiRes<StationMonthKpi>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<StationMonthKpi>>>>() {
        };

        JsonRootBean<List<StationRealKpiRes<StationMonthKpi>>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<StationMonthKpi>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<StationMonthKpi> stationMonthKpis = item.getDataItemMap();
                for (StationMonthKpi stationMonthKpi : stationMonthKpis) {
                    stationMonthKpi.setStationCode(item.getStationCode());
                    Date resCollectTime = DateUtil.date(item.getCollectTime());
                    stationMonthKpi.setCollectTime(resCollectTime);
                    stationMonthKpiService.insertStationMonthKpi(stationMonthKpi);
                }
            }
        }
        return success(result);
    }

    //电站年数据接口
    @GetMapping("/getKpiStationYear")
    public AjaxResult getKpiStationYear(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes,
                                        @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getKpiStationYear";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        datamap.put("collectTime", collectTime);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        result=replaceJson(result);
        TypeReference<JsonRootBean<List<StationRealKpiRes<StationYearKpi>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<StationYearKpi>>>>() {
        };
        JsonRootBean<List<StationRealKpiRes<StationYearKpi>>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<StationYearKpi>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<StationYearKpi> stationYearKpis = item.getDataItemMap();
                for (StationYearKpi stationYearKpi : stationYearKpis) {
                    Date resCollectTime = DateUtil.date(item.getCollectTime());
                    stationYearKpi.setCollectTime(resCollectTime);
                    stationYearKpiService.insertStationYearKpi(stationYearKpi);
                }
            }
        }
        return success(result);
    }

    //设备列表
    @GetMapping("/getDevList")
    public AjaxResult getDevList(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes) {
        String url = domain + "thirdData/getDevList";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        TypeReference<JsonRootBean<List<Devices>>> typeRef = new TypeReference<JsonRootBean<List<Devices>>>() {
        };
        JsonRootBean<List<Devices>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<Devices> list = jsonRootBean.getData();
            for (Devices item : list) {
                item.setDevId(item.getId());
                devicesService.insertDevices(item);
            }
        }
        return success(result);
    }

    //设备实时数据
    @GetMapping("/getDevRealKpi")
    public AjaxResult getDevRealKpi(@ApiParam(name = "devIds", value = "设备编号列表", required = false) @RequestParam(value = "devIds", defaultValue = "1000000034363468") String devIds,
                                    @ApiParam(name = "devTypeId", value = "设备类型ID", required = false) @RequestParam(value = "devTypeId", defaultValue = "1") Integer devTypeId) {
        String url = domain + "thirdData/getDevRealKpi";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("devIds", devIds);
        //1：组串式逆变器
        //10：环境监测仪
        //17：关口电表
        //38：户用逆变器
        //39：储能
        //41：ESS
        //47：户用电表
        datamap.put("devTypeId", devTypeId);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        if (result.contains("true")) {
            result=replaceJson(result);

            switch (devTypeId) {
                case 1:
                    processDeviceInfo(1, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo1>>>>() {}, deviceInfo1Service,result);
                    break;
                case 10:
                    processDeviceInfo(10, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo10>>>>() {}, deviceInfo10Service,result);
                    break;
                case 17:
                    processDeviceInfo(17, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo17>>>>() {}, deviceInfo17Service,result);
                    break;
                case 38:
                    processDeviceInfo(38, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo38>>>>() {}, deviceInfo38Service,result);
                    break;
                case 39:
                    processDeviceInfo(39, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo39>>>>() {}, deviceInfo39Service,result);
                    break;
                case 41:
                    processDeviceInfo(41, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo41>>>>() {}, deviceInfo41Service,result);
                    break;
                case 47:
                    processDeviceInfo(47, new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceInfo47>>>>() {}, deviceInfo47Service,result);
                    break;

                default:
                    break;
            }
        }

        return success(result);
    }

    private <T> void processDeviceInfo(int devTypeId, TypeReference<JsonRootBean<List<StationRealKpiRes<T>>>> typeRef, Object service,String result) {
        JsonRootBean<List<StationRealKpiRes<T>>> jsonRootBean = JSON.parseObject(result, typeRef);
        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<T>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<T> deviceInfos = item.getDataItemMap();
                for (T deviceInfo : deviceInfos) {
                    try {
                        Method setDevIdMethod = deviceInfo.getClass().getMethod("setDevId", Long.class);
                        setDevIdMethod.invoke(deviceInfo, item.getDevId());

                        Method insertMethod = service.getClass().getMethod("insertDeviceInfo" + devTypeId, deviceInfo.getClass());
                        insertMethod.invoke(service, deviceInfo);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    //设备日数据
    @GetMapping("/getDevKpiDay")
    public AjaxResult getDevKpiDay(@ApiParam(name = "devIds", value = "设备编号列表", required = false) @RequestParam(value = "devIds", defaultValue = "1000000034360254") String devIds,
                                   @ApiParam(name = "devTypeId", value = "设备类型ID", required = false) @RequestParam(value = "devTypeId", defaultValue = "1") Integer devTypeId,
                                   @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getDevKpiDay";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("collectTime", collectTime);
        datamap.put("devIds", devIds);
        //1：组串式逆变器
        //38：户用逆变器
        //39：储能
        //41：ESS
//        Integer devTypeId = 1;
        datamap.put("devTypeId", devTypeId);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();
        if (result.contains("true")) {
            addData(result,devTypeId,1);
        }
        return success(result);
    }
    protected  void addData(String result,Integer devTypeId,Integer dataType){
        result=replaceJson(result);
        switch (devTypeId) {
            case 1:
            case 38:
                TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceDayInfo1>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceDayInfo1>>>>() {};
                JsonRootBean<List<StationRealKpiRes<DeviceDayInfo1>>> jsonRootBean = JSON.parseObject(result, typeRef);
                List<StationRealKpiRes<DeviceDayInfo1>> list = jsonRootBean.getData();
                for (StationRealKpiRes item : list) {
                    List<DeviceDayInfo1> deviceDayInfo1s = item.getDataItemMap();
                    for (DeviceDayInfo1 deviceDayInfo1 : deviceDayInfo1s) {
                        deviceDayInfo1.setDataType(dataType);
                        deviceDayInfo1.setDevTypeId(devTypeId);
                        deviceDayInfo1.setDevId(item.getDevId());
                        Date resCollectTime = DateUtil.date(item.getCollectTime());
                        deviceDayInfo1.setCollectTime(resCollectTime);
                        deviceDayInfo1Service.insertDeviceDayInfo1(deviceDayInfo1);
                    }
                }
                break;
            case 39:
            case 41:
                TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceDayInfo39>>>> deviceDayInfo39TypeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceDayInfo39>>>>() {
                };
                JsonRootBean<List<StationRealKpiRes<DeviceDayInfo39>>> deviceDayInfo39JsonRootBean = JSON.parseObject(result, deviceDayInfo39TypeRef);
                List<StationRealKpiRes<DeviceDayInfo39>> deviceDayInfo39List = deviceDayInfo39JsonRootBean.getData();
                for (StationRealKpiRes item : deviceDayInfo39List) {
                    List<DeviceDayInfo39> deviceDayInfo39s = item.getDataItemMap();
                    for (DeviceDayInfo39 deviceDayInfo39 : deviceDayInfo39s) {
                        deviceDayInfo39.setDataType(dataType);
                        deviceDayInfo39.setDevTypeId(devTypeId);
                        deviceDayInfo39.setDevId(item.getDevId());
                        Date resCollectTime = DateUtil.date(item.getCollectTime());
                        deviceDayInfo39.setCollectTime(resCollectTime);
                        deviceDayInfo39Service.insertDeviceDayInfo39(deviceDayInfo39);
                    }
                }
                break;
            default:
                break;
        }
    }

    //设备月数据
    @GetMapping("/getDevKpiMonth")
    public AjaxResult getDevKpiMonth(@ApiParam(name = "devIds", value = "设备编号列表", required = false) @RequestParam(value = "devIds", defaultValue = "1000000034360254") String devIds,
                                     @ApiParam(name = "devTypeId", value = "设备类型ID", required = false) @RequestParam(value = "devTypeId", defaultValue = "1") Integer devTypeId,
                                     @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getDevKpiMonth";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("devIds",devIds);
        datamap.put("collectTime", collectTime);
        //1：组串式逆变器
        //38：户用逆变器
        //39：储能
        //41：ESS
//        Integer devTypeId = 1;
        datamap.put("devTypeId", devTypeId);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        if(result.contains("true")){
            addData(result,devTypeId,2);
        }
        return success(result);
    }

    //设备年数据
    @GetMapping("/getDevKpiYear")
    public AjaxResult getDevKpiYear(@ApiParam(name = "devIds", value = "设备编号列表", required = false) @RequestParam(value = "devIds", defaultValue = "1000000034360254") String devIds,
                                    @ApiParam(name = "devTypeId", value = "设备类型ID", required = false) @RequestParam(value = "devTypeId", defaultValue = "1") Integer devTypeId,
                                    @ApiParam(name = "collectTime", value = "采集时间毫秒数", required = false) @RequestParam(value = "collectTime", defaultValue = "1724194690000") String collectTime) {
        String url = domain + "thirdData/getDevKpiYear";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("devIds", devIds);
        datamap.put("collectTime", collectTime);
        //1：组串式逆变器
        //38：户用逆变器
        //39：储能
        //41：ESS
//        Integer devTypeId = 1;
        datamap.put("devTypeId", devTypeId);
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        if(result.contains("true")){
            addData(result,devTypeId,3);
        }
        return success(result);
    }

    //设备告警
    @GetMapping("/getAlarmList")
    public AjaxResult getAlarmList(@ApiParam(name = "stationCodes", value = "电站编号", required = false) @RequestParam(value = "stationCodes", defaultValue = "NE=34349181") String stationCodes,
                                   @ApiParam(name = "beginTime", value = "查询开始时间毫秒数", required = false) @RequestParam(value = "beginTime", defaultValue = "1724194690000") String beginTime,
                                   @ApiParam(name = "endTime", value = "查询结束时间毫秒数", required = false) @RequestParam(value = "endTime", defaultValue = "1724375744341") String endTime,
                                   @ApiParam(name = "language", value = "语言", required = false) @RequestParam(value = "language", defaultValue = "zh_CN") String language,
                                   @ApiParam(name = "levels", value = "告警级别", required = false) @RequestParam(value = "levels", defaultValue = "") String levels,
                                   @ApiParam(name = "devTypes", value = "设备类型", required = false) @RequestParam(value = "devTypes", defaultValue = "") String devTypes) {
        String url = domain + "thirdData/getAlarmList";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("stationCodes", stationCodes);
        datamap.put("beginTime", beginTime);
        datamap.put("endTime", endTime);
        datamap.put("language", language);
        if (ObjUtil.isNotEmpty(levels)) {
            datamap.put("levels", levels);
        }
        if (ObjUtil.isNotEmpty(devTypes)) {
            datamap.put("devTypes", devTypes);
        }
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceAlarms>>>> typeRef = new TypeReference<JsonRootBean<List<StationRealKpiRes<DeviceAlarms>>>>() {
        };
        JsonRootBean<List<StationRealKpiRes<DeviceAlarms>>> jsonRootBean = JSON.parseObject(result, typeRef);

        if (jsonRootBean.getSuccess()) {
            List<StationRealKpiRes<DeviceAlarms>> list = jsonRootBean.getData();
            for (StationRealKpiRes item : list) {
                List<DeviceAlarms> stationHourKpis = item.getDataItemMap();
                for (DeviceAlarms deviceAlarms : stationHourKpis) {
                    deviceAlarmsService.insertDeviceAlarms(deviceAlarms);
                }
            }
        }
        return success(result);
    }

    //设备历史数据 下线接口
    @GetMapping("/getDevFiveMinutes")
    public AjaxResult getDevFiveMinutes() {
        String url = domain + "thirdData/getDevFiveMinutes";
        Map<String, Object> datamap = new HashMap<>();
        datamap.put("devIds", "NE=34349181");
        datamap.put("devTypes", "1");
        datamap.put("collectTime", "1724194690000");
        String result = HttpRequest.post(url)
                .header(tokenName, xsrfToken)//头信息
                .body(JSONUtils.toJSONString(datamap))//表单内容
                .timeout(20000)//超时，毫秒
                .execute().body();

        return success(result);
    }
}
